package com.dc.service.impl;

import com.dc.base.contants.GlobalVarible;
import com.dc.base.em.EnumRole;
import com.dc.base.exception.BusinessException;
import com.dc.base.exception.ResponseCode;
import com.dc.base.pojo.BaseModel;
import com.dc.base.rsa.MD5DES;
import com.dc.mapper.SysLoginLogDao;
import com.dc.mapper.SysRoleDao;
import com.dc.mapper.SysUserDao;
import com.dc.pojo.SysLoginLog;
import com.dc.pojo.SysMenu;
import com.dc.pojo.SysRole;
import com.dc.pojo.SysUser;
import com.dc.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author
 * @Description TODO
 * @date 2018-11-12 15:24
 */
@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private SysUserDao userDao;
    @Autowired
    private SysLoginLogDao loginLogDao;
    @Autowired
    private SysRoleDao sysRoleDao;
    /**
     * @return void
     * @title:<h3> 登录 <h3>
     * @author: Enzo
     * @date: 2018-11-12 15:24
     * @params [loginLog, baseModel]
     **/
    public void login(SysLoginLog loginLog, BaseModel baseModel) throws Exception {
        //1.根据用户名查询用户信息
        SysUser user = userDao.selectUserByAccount(loginLog.getUser_account());
        //2.判断帐号是否存在
        if (user == null) {//如果用户信息不存在，说明帐号不存在
            throw new BusinessException(ResponseCode.LOGIN_PASSWORD.getCode(), ResponseCode.LOGIN_PASSWORD.getMessage());
        } else {//说明存在帐号
            //3.验证帐号是否激活
            if (user.getState() == 1) {//说启用
                //4.判断密码是否正确
                if(user.getPassword().equals(MD5DES.encrypt(loginLog.getUser_password()))){//如果密码正确，登录成功
                    loginLog.setState(1);
                    baseModel.setResultCode(0);
                    baseModel.setMessage("登录成功");
                    user.setPassword(null);//密码等敏感数据不返回
                    baseModel.setData(user);
                    loginLog.setUser_password(MD5DES.encrypt(loginLog.getUser_password()));//登录成功的密码不保存在登录日志
                }else{//帐号或密码错误
                    throw new BusinessException(ResponseCode.LOGIN_PASSWORD.getCode(), ResponseCode.LOGIN_PASSWORD.getMessage());
                }
            }else{//没启用
                loginLog.setState(4);
                loginLog.setUser_password(null);//未激活的密码不保存在登录日志
                throw new BusinessException(ResponseCode.LOGIN_INACTIVE.getCode(), ResponseCode.LOGIN_INACTIVE.getMessage());

            }
        }
        //5.不管登录成功否，记录登录日志
        int count=loginLogDao.insert(loginLog);
        if(count==0){//新增登录日志失败
            throw new BusinessException(ResponseCode.ADD_LOG.getCode(), ResponseCode.ADD_LOG.getMessage());
        }
    }
    public List<SysMenu> selectAllMenu(){
        List<SysMenu> sysMenus = this.sysRoleDao.selectMenuAll();
        GlobalVarible.mapCodeMenu = new HashMap();
        GlobalVarible.mapIdCode = new HashMap();
        Iterator i$ = sysMenus.iterator();

        while(i$.hasNext()) {
            SysMenu menu = (SysMenu)i$.next();
            GlobalVarible.mapCodeMenu.put(menu.getCode(), menu);
            GlobalVarible.mapIdCode.put(menu.getMenu_id(), menu.getCode());
        }

        return sysMenus;
    }
    public void selectEnumRoleMenu(BaseModel baseModel) {
        //如果全局变量中是空就去查询所有的权限
        if (GlobalVarible.mapCodeMenu == null) {
            this.selectAllMenu();
        }

        List<Map<String, String>> listMenu = new ArrayList();
        EnumRole[] arr = EnumRole.values();
        int len = arr.length;

        for(int i = 0; i < len; ++i) {
            EnumRole em = arr[i];
            Map<String, String> map = new HashMap();
            if (GlobalVarible.mapCodeMenu.get(em.getCode() + "") != null) {
                map.put("code", em.getCode() + "");
                map.put("name", em.getName());
                map.put("roleValue", em.getRoleValue());
                listMenu.add(map);
            }
        }

        baseModel.setData(listMenu);
    }
}
